package project

import (
	"awesomeTask/application/client"
	"awesomeTask/application/models/programe"
	"awesomeTask/application/resource"
	"awesomeTask/application/variable"
	awesomeTask "awesomeTask/commom"
	"awesomeTask/system/helper"
)

const TABLE_NAME = "t_project"

func CreateProject(projectname string, description string, id interface{}, groupId string, uid string) bool {
	params := map[string]interface{}{
		"project_name": projectname,
		"description":  description,
		"id":           id,
		"group_id":     groupId,
		"update_time":  awesomeTask.Now(),
		"uid":          uid,
	}
	if id == nil {
		params["id"] = nil
		params["project_uname"] = buildProjectName(6)
		params["create_time"] = awesomeTask.Now()
	}
	if awesomeTask.InsertOrUpdate(TABLE_NAME, params) {
		go func() {
			resource.PythonEnv(params["project_uname"].(string))
			info := map[string]interface{}{
				"projectName": params["project_uname"].(string),
			}
			//通知其他节点构建环境
			client.SendEventToAll(variable.ON_BUILD_ENV, info)
		}()
		return true
	}
	return false
}
func buildProjectName(length int) string {
	res := ""
	str := "qwertyuiopasdfghjklzxcvbnm"
	for i := 0; i < length; i++ {
		index := helper.RandInt(0, len(str)-1)
		res += str[index : index+1]
	}
	return res
}
func GetProject(uid string) []map[string]interface{} {
	data := awesomeTask.Query("select project_name as title,description,id from "+TABLE_NAME+" where uid=? and delete_time is null", uid)
	data = programe.GetProgrameBeTree(data)
	return data
}
